<?php
/**
 * 商户列表
 * @author Administrator
 *
 */
class shoplist extends CControllerOwnerBase{
	
	public function show()
	{
		$cName = $this->getRequestData("txt_c_name","get");
		$searchType = $this->getRequestData("r_search_type","get","c");
		$s_type = $this->getRequestData("t","get");  //类别
		$isTop = $this->getRequestData("top","get",0);
		
		$model = new shopModel();
		
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 12;
		$c = 0;
		
		$sid = "";
		$urlParam["r_search_type"] = $searchType;
		
		$where = "t.del_flag=0 and sp.del_flag=0 and t.shop_id=sp.shop_id";
		$order = "p_id desc";
		
		$noRecord = false;
		if($cName && $searchType=="c")
		{
			$sid = $model->getShopProductByC($cName);
			$urlParam["txt_c_name"] = $cName;
			
			if($sid)
			{
				$where .= " and t.shop_id in (".$sid.")";
			}
			else{
				$noRecord = true;
			}
		}
		else if($cName && $searchType=="s")
		{
			$urlParam["txt_c_name"] = $cName;
			$where .= " and t.shop_name like '%".$cName."%'";
		}
		
		if($isTop)
		{
			$order = "sp.is_top desc,sp.sort_num desc,".$order;
			$urlParam["top"] = 1;
		}
		
		$topHtml = "";
			
		$sTypeList = Config::get("shop_product_type");
		
		$isvalid = false;
		foreach($sTypeList as $item)
		{
			if($item["id"]==$s_type)
			{
				$isvalid = true;
			}
		}
		if($isvalid)
		{
			$where .= " and p_type=".$s_type;
			
			$urlParam["t"] = $s_type;
		}
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_c_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		if($noRecord)
		{
			$topHtml = "没有找到符合条件的商户!";
		}
		else{
			
			$list = $model->selectPage($pageIndex,$pageSize,"sp.shop_id,sp.p_name,sp.pic_path,t.shop_simple_name",$where,$order,"",$c,"t_shop t,t_shop_product sp");
			$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);
		
			if($c==0)
			{
				$topHtml = "没有找到符合条件的商户!";
			}
			else{
				$topHtml = "<table><tr>";
				$i=0;
				foreach ($list as $item)
				{
					$i++;
					$topHtml .= '
						<td width="310px" align="center">
						<table>
							<tr>
								<td align="center" style="width:290px; height:199px;"><a href="/shoplist/detail?id='.$item["shop_id"].'"><img id="img_'.$item["shop_id"].'" imgsrc="'.$item["pic_path"].'" /></a></td>
							</tr>
							<tr>
								<td align="left"><div class="shop_list_div"><span class="color_b">['.$item["shop_simple_name"].']</span><a href="/shoplist/detail?id='.$item["shop_id"].'">'.$item["p_name"].'</a></div></td>
							</tr>
						</table>
						</td>';
						
					if($i>0 && $i%3==0 && $i<count($list))
					{
						$topHtml .= "</tr><tr>";
					}
				}
				$topHtml .= "</tr></table>";
				$topHtml .= "<table width='100%'><tr><td align='right'>".$pageStr."</td></tr>";
				$topHtml .= "</table>";
			}
		}
		
		$this->setJsBlock("shopImpThumb('shop_list');");
		$this->render("shoplist",array("spHtml"=>$topHtml,"cName"=>$cName,"search_type"=>$searchType));
	}
	
	public function detail()
	{
		$id = $this->getRequestData("id","get");
		$model=new shopModel();
		
		$modelInfo = $model->selectByWhere(
			"t_shop s, t_shop_product sp",
			"shop_simple_name, p_name",array("shop_id"=>array("v"=>$id,"table"=>"s")),"s.shop_id=sp.shop_id and s.del_flag=0");
		
		if($modelInfo)
		{
			$c = $model->getShopDesc($id);
			$content = "";
			if($c && is_array($c) && isset($c["shop_desc"]))
			{
				$content = stripslashes($c["shop_desc"]);
			}
			$modelInfo["shop_desc"] = $content;
		}
		
		$this->render("shopdetail",array("modelInfo"=>$modelInfo));
	}
}